var attnSetting = {
    events:{
        ".attnSetting-body .update click" : "updateRule",
        ".attnSetting-body .save-rule click" : "saveRule",
        ".attnSetting-body #addRule click" : "addRule",
    },
	init : function(){
		var _this = this;
        document.title = "考勤设置 - 考勤管理";
        var temp = Util.template($("#attnSetting-template").html(),{});
        $(".commonBody-rightBody").html(temp);
        Util.event.call(_this,_this.events)
        this.getRules();
        this.getDeptList();
        $("#addRule").click();
	},
	getRules : function(){
        Util.ajaxRequest({
            url: interfaceURL.getRuleList(),
            showLoading : false,
            success : function(data){
                if(data.code == 200){
                    var columns = data.result;
                    $.each(columns,function(i,v){
                        v.operation = '<button class="btn btn-blue update">修改</button>';
                    })
                    $(".attn-rules-table").cakeTable({
                        head: [
                            {field: "name",name: "名称",size:6},
                            {field: "operation",name: "操作",type:2}
                        ],
                        rowData : [{key:"id",attrName:"data-id"}],
                        sort: false,
                        columns: columns,
                        showPage:false
                    })
                }
            }
        })
	},
    getDeptList : function(){
        var that = this;
        Util.ajaxRequest({
            url: interfaceURL.getDeptList(),
            showLoading : false,
            success : function(data){
                if(data.code == 200){
                    var zTreeObj,
                        setting = {
                            check: {
                                enable: true,
                                chkboxType: { "Y": "s", "N": "" }
                            },
                            data: {
                                simpleData: {
                                    enable: true
                                }
                            }
                        },
                        treeNode = data.result;
                    that.zTreeObj = $.fn.zTree.init($("#treeList"), setting, treeNode);
                    that.zTreeObj.expandAll(true);
                }
            }
        })
    },
    addRule : function(){
        if(this.zTreeObj){
            this.zTreeObj.checkAllNodes(false);//取消当前部门树所有的选中状态
        }
        var temp = Util.template($("#rule-detail-template").html(),{data:null});
        $(".add-attn-rule").html(temp);
        this.initTimePicker();
    },
    updateRule : function(ele){
        var that = this;
        var ruleId = $(ele).parents("tr").attr("data-id");
        Util.ajaxRequest({
            url: interfaceURL.getRuleById(),
            data : {ruleId:ruleId},
            showLoading : false,
            success : function(data){
                if(data.code == 200){
                    var temp = Util.template($("#rule-detail-template").html(),{data:data.result});
                    $(".add-attn-rule").html(temp);
                    that.checkDepartment(data.result.depIds);
                    that.initTimePicker();
                }
            }
        })
    },
    checkDepartment : function(depIds){
        this.zTreeObj.checkAllNodes(false);//取消当前部门树所有的选中状态
        for(var i=0;i<depIds.length;i++){
            var node = this.zTreeObj.getNodeByParam("id",depIds[i]);
            this.zTreeObj.checkNode(node, true, false);
        }
    },
    saveRule : function(ele){
        var that = this;
        var obj = Util.getData($(".add-attn-rule"));
        obj.id = $(ele).attr("data-id");
        obj.workday = "";
        $(".property[name=workday]").find(".checked").each(function(){
            obj.workday = obj.workday == "" ? $(this).attr("value") : obj.workday + "," + $(this).attr("value");
        });
        obj.depIds = "";
        var nodes = this.zTreeObj.getCheckedNodes(true);
        $.each(nodes, function(i,v){
            obj.depIds = obj.depIds == "" ? v.id : obj.depIds + "," + v.id;
        })
        if(Util.isEmpty(obj.name,obj.checkIn,obj.checkOut,obj.workday,obj.depIds)){
            pop.alert("规则名称、工作时间段、工作日、执行对象是必填项，请填写完整后再保存！");
            return false;
        }
        var sh=parseInt(obj.checkIn.split(":")[0]),
            sm=parseInt(obj.checkIn.split(":")[1]),
            eh=parseInt(obj.checkOut.split(":")[0]),
            em=parseInt(obj.checkOut.split(":")[1]);
        if(eh<sh||(eh>=sh && em<sm)){
            pop.alert("签到时间必须早于签退时间！");
            return false;
        }
        Util.ajaxRequest({
            url: interfaceURL.saveRule(),
            data : obj,
            success : function(data){
                if(data.code == 200){
                    that.getRules();
                }
            }
        })
    },
    initTimePicker : function(){
        $(".timepicker").datetimepicker({
            initialDate : new Date().format("yyyy-MM-dd") + " 00:00:00",
            startDate : new Date().format("yyyy-MM-dd") + " 00:00:00",
            endDate : new Date().format("yyyy-MM-dd") + " 23:59:59",
            format: 'hh:ii',
            autoclose: true,
            language: "zh-CN",
            pickerPosition: "bottom-left",
            minView : 0,
            maxView : 0,
            startView : 0,
        });
    }
};